package com.soft.bams.entity;

import com.soft.bams.exception.MoneyException;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/22
 */
public class LoanSavingAccount extends SavingAccount implements Loanable{
    private long loanAmount;

    public LoanSavingAccount(Long id, String password, String name, String personid, String email, double balance,long loanAmount) {
        super(id, password, name, personid, email, balance);
        this.loanAmount = loanAmount;
    }
    public long getLoanAmount() {
        return loanAmount;
    }
    public void setLoanAmount(long loanAmount) {
        this.loanAmount = loanAmount;
    }
    @Override
    public String toString() {
        return "LoanSavingAccount{" + "loanAmount=" + loanAmount + "} " + super.toString();
    }
    //贷款
    @Override
    public Account requestLoan(double money) throws MoneyException {
        if(money<=0){
//            System.out.println("贷款金额要>0");
//            return this;
        throw new MoneyException("贷款金额要>0");
        }
        this.setBalance(this.getBalance()+money);
        this.setLoanAmount((long)money);
        return this;
    }
    //还贷款
    @Override
    public Account payLoan(double money) throws MoneyException {
        if(money<=0){
//            System.out.println("还款金额要>0");
//            return this;
            throw new MoneyException("贷款金额要>0");
        }else if(money>this.getLoanAmount()){
//            System.out.println("还款金额大于贷款金额！");
//            return this;
            throw new MoneyException("还款金额大于贷款金额！");
        }else if(money>this.getBalance()){
//            System.out.println("您的余额小于贷款金额！");
//            return this;
            throw new MoneyException("您的余额小于贷款金额！");
        }
        loanAmount=(long)(loanAmount-money);
        this.setBalance(this.getBalance()-money);
        return this;
    }
}
